from selenium import webdriver
from time import sleep
from PIL import Image
# from .api import main
from selenium.common.exceptions import NoSuchElementException
from .login import login_test
from .verification import main
import re
from selenium.webdriver.chrome.options import Options


def open_browser(browser_name, url):
    driver = None
    chrome_options = Options()
    chrome_options.add_argument("--headless")
    chrome_options.add_argument('--no-sandbox')
    chrome_options.add_argument('--disable-dev-shm-usage')
    chrome_options.add_argument('--window-size=1920,1080')
    if browser_name == "chrome":
        driver = webdriver.Chrome(chrome_options=chrome_options)
    if browser_name == "ff":
        driver = webdriver.Firefox()
    if browser_name == "ie":
        driver = webdriver.Ie()
    driver.maximize_window()
    driver.implicitly_wait(10)
    driver.get(url)
    return driver


class TestKeys(object):

    def __init__(self, browser_name, url, username, password, new_password):
        self.driver = open_browser(browser_name, url)
        self.username = username
        self.password = password
        self.new_password = new_password

    def webui1(self, list_dicts):
        # 登录
        driver = login_test(self.username, self.driver, self.new_password)
        # 系统管理
        driver.find_element_by_xpath('//*[@id="app"]/section/section/aside/div/ul/div/li[6]').click()
        # 时间同步
        driver.find_element_by_xpath('//*[@id="app"]/section/section/aside/div/ul/div/li[6]/ul/div/li[6]').click()
        sleep(1)
        returns_list = []
        for dicts in list_dicts:
            if dicts["host_ip"] is not None:
                # 目标主机地址
                driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/main/form/div[1]/div/div/div/div[1]/input').send_keys(dicts["host_ip"])
                sleep(1)
            if dicts["host_port"] is not None:
                # 目标主机端口
                driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/main/form/div[2]/div/div/div/div/input').send_keys(dicts["host_port"])
                sleep(1)
            if dicts["host_user"] is not None:
                # 目标主机用户
                driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/main/form/div[3]/div/div/div/div/input').send_keys(dicts["host_user"])
                sleep(1)
            if dicts["host_password"] is not None:
                # 目标主机密码
                driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/main/form/div[4]/div/div/div/div/input').send_keys(dicts["host_password"])
                sleep(1)
            # 开始同步
            driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/main/form/div[5]/div/button').click()
            sleep(4)
            if dicts["host_ip"] is None:
                error_text = driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/main/form/div[1]/div/div/div/div[2]').text
                print(error_text)
                returns_list.append(error_text)
                driver.refresh()
                continue
            if dicts["host_port"] is None:
                error_text = driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/main/form/div[2]/div/div/div/div[2]').text
                print(error_text)
                returns_list.append(error_text)
                driver.refresh()
                continue
            if dicts["host_user"] is None:
                error_text = driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/main/form/div[3]/div/div/div/div[2]').text
                print(error_text)
                returns_list.append(error_text)
                driver.refresh()
                continue
            if dicts["host_password"] is None:
                error_text = driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/main/form/div[4]/div/div/div/div[2]').text
                print(error_text)
                returns_list.append(error_text)
                driver.refresh()
                continue
            if re.match(r"^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$", dicts["host_ip"]):
                print("正确的ip")
            else:
                print("错误的ip")
                error_text = driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/main/form/div[1]/div/div/div/div[2]').text
                print(error_text)
                returns_list.append(error_text)
                driver.refresh()
                continue
            if int(dicts["host_port"]) > 65535:
                error_text = driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/main/form/div[2]/div/div/div/div[2]').text
                print(error_text)
                returns_list.append(error_text)
                driver.refresh()
                continue
            if len(dicts["host_user"]) < 3 or len(dicts["host_user"]) > 32:
                error_text = driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/main/form/div[3]/div/div/div/div[2]').text
                print(error_text)
                returns_list.append(error_text)
                driver.refresh()
                continue
            returns_list.append("同步成功")
            driver.refresh()
            continue
        print(returns_list)
        return returns_list

    def quit(self):
        """
        浏览器退出
        :return:
        """
        self.driver.quit()
